package ace.module.sms.core.impl.service.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.module.common.api.constant.CommonConstants;
import ace.module.sms.api.constant.SmsConstant;
import ace.module.sms.api.enums.SmsApplicationStatusEnum;
import ace.module.sms.api.enums.SmsSupplierTypeEnum;
import ace.module.sms.core.impl.dao.aggregate.SmsApplicationAggregate;
import ace.module.sms.core.impl.dao.entity.SmsApplication;
import ace.module.sms.core.impl.dao.entity.SmsApplicationConfig;
import ace.module.sms.core.impl.dao.entity.SmsSupplier;
import ace.module.sms.core.impl.dao.repository.SmsApplicationRepository;
import ace.module.sms.core.impl.service.SmsInitService;
import java.time.Instant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
* module init.
*
* @author Caspar
*/
@Slf4j
@AllArgsConstructor
@Component
public class SmsInitServiceImpl implements SmsInitService {
  private final SmsApplicationAggregate smsApplicationAggregate;
  private final SmsApplicationRepository smsApplicationRepository;

  @Override
  public void init() {
    if (this.smsApplicationRepository.getById(SmsConstant.DEFAULT_APP_ID) != null) {
      return;
    }

    Long smsSupplierConfigId = SmsConstant.DEFAULT_SMS_SUPPLIER_CONFIG_ID;
    SmsApplication smsApplication =
        SmsApplication.builder()
            .id(SmsConstant.DEFAULT_APP_ID)
            .key(SmsConstant.DEFAULT_ID_KEY)
            .remark("")
            .status(SmsApplicationStatusEnum.NORMAL.getCode())
            .name("default")
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    SmsApplicationConfig smsApplicationConfig =
        SmsApplicationConfig.builder()
            .id(SmsConstant.DEFAULT_APP_ID)
            .smsAppId(SmsConstant.DEFAULT_APP_ID)
            .smsSupplierId(smsSupplierConfigId)
            .templateContent("{\"phone\":\"${phoneNumber}\"}")
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    SmsSupplier smsSupplier =
        SmsSupplier.builder()
            .id(smsSupplierConfigId)
            .name(CommonConstants.DEFAULT_TITLE)
            .remark(CommonConstants.DEFAULT_REMARK)
            .type(SmsSupplierTypeEnum.TEST.getCode())
            .config("test")
            .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
            .createTime(Instant.now().toEpochMilli())
            .updateTime(Instant.now().toEpochMilli())
            .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
            .build();

    this.smsApplicationAggregate.insert(smsApplication, smsApplicationConfig, smsSupplier);

    log.info("init data success");
  }
}
